﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.CarClub
{
    public class CarInfo : IHttpHandler
    {
        private Jy123.Model.User_Main user;
        private bool isVenues = false;
        private Jy123.Model.Module_Main module;
        public void ProcessRequest(HttpContext context)
        {
            BindList(context);
        }

        private void BindList(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            string pType = context.Request.QueryString["PType"];
            string action = context.Request["Action"];
            string strVId = context.Request.QueryString["ClubId"];
            if (string.IsNullOrEmpty(strVId))
            {
                strVId = context.Request["ClubId"];
            }
            if (!string.IsNullOrEmpty(strVId))
            {
                int moduleId = int.Parse(strVId);
                Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                module = BLLModule.GetModel(moduleId);
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Jy123.Model.Public_JType pJType = BLLJType.GetModel(module.FJType.Value);
                int jFather = pJType.JFather.Value;
                if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Venues_Main))
                {
                    isVenues = true;
                }
                user = BLLUser.GetModel(module.FUser.Value);
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                if (!string.IsNullOrEmpty(type))
                {
                    string html = string.Empty;
                    switch (type)
                    {
                        case "Car":
                            Jy123.BLL.Club_Cars BLLAR = new Jy123.BLL.Club_Cars();
                            string strWhere = string.Format("FClub = {0}", strVId);
                            List<Jy123.Model.Club_Cars> listRecord = BLLAR.GetModelList(strWhere);
                            html = BindDetail(listRecord, context);
                            break;
                        case "CarStore":
                            Jy123.BLL.Public_Cars BLLCars = new Jy123.BLL.Public_Cars();
                            List<Jy123.Model.Public_Cars> listCars = BLLCars.GetModelList(string.Empty);
                            html = BindDetail(listCars, context);
                            break;
                    }
                    if (string.IsNullOrEmpty(html))
                    {
                        html = "暂无此类商品";
                    }
                    context.Response.Write(html);
                }
                else if (!string.IsNullOrEmpty(action))
                {
                    string strId = context.Request["Id"];
                    if (!string.IsNullOrEmpty(strId))
                    {
                        int id = int.Parse(strId);
                        BLL.Public_Cars BLLPCars = new Jy123.BLL.Public_Cars();
                        BLL.Club_Cars BLLARecord = new Jy123.BLL.Club_Cars();
                        Model.Public_Cars car = BLLPCars.GetModel(id);

                        if (action.Equals("Annexe"))
                        {
                            Model.Club_Cars clubCars = new Jy123.Model.Club_Cars();
                            clubCars.FClubCarsType = id;
                            clubCars.FClub = moduleId;
                            BLLARecord.Add(clubCars);
                            user.Cash = Convert.ToInt32(user.Cash.Value - car.Price.Value);
                            BLLUser.Update(user);
                            if (jFather == Convert.ToInt32(BLL.Public.EnumConfig.JyType.Club_Main))
                            {
                                Model.Club_Main club = BLLClub.GetModel(moduleId);
                                if (club.Assets.HasValue)
                                {
                                    club.Assets = club.Assets.Value + car.Price.Value;
                                }
                                else
                                {
                                    club.Assets = car.Price.Value;
                                }
                                BLLClub.Update(club);
                            }

                        }
                        else
                        {
                            string strRWhere = string.Format("FClub = {0} AND IsUsed = 1", strVId);

                            List<Jy123.Model.Club_Cars> listRecord = BLLARecord.GetModelList(strRWhere);
                            Jy123.Model.Club_Cars currentRecord = BLLARecord.GetModel(id);
                            if (listRecord.Count > 0)
                            {
                                Jy123.Model.Club_Cars cRecord = listRecord[0];
                                cRecord.IsUsed = false;
                                currentRecord.IsUsed = true;
                                BLLARecord.Update(cRecord);
                                BLLARecord.Update(currentRecord);
                            }

                        }
                    }
                }

                Model.Club_Main sClub = BLLClub.GetModel(moduleId);
                string strValue = (Convert.ToInt32(sClub.Assets.Value)).ToString();

                string strAssets = string.Format("总资产：<span class=\"FontRedB\">{0}</span>", strValue);
                user = BLLUser.GetModel(module.FUser.Value);
                string strCash = string.Format("现金：<span class=\"FontRedB\">{0}</span>", user.Cash.Value.ToString());
                StringBuilder sbString = new StringBuilder();
                sbString.Append("<script type='text/javascript' language='javascript'>");
                sbString.AppendFormat("$('#assetsLi').html('{0}');", strAssets);
                sbString.AppendFormat("$('#cashLi').html('{0}');", strCash);
                sbString.Append("</script>");
                context.Response.Write(sbString.ToString());
            }
        }

        private string BindDetail(List<Jy123.Model.Club_Cars> listRecord, HttpContext context)
        {
            StringBuilder strResult = new StringBuilder();
            Jy123.BLL.Public_Cars BLLPCars = new Jy123.BLL.Public_Cars();
            List<Jy123.Model.Public_Cars> listCars = BLLPCars.GetModelList(string.Empty);
            Dictionary<int, Jy123.Model.Public_Cars> dicPCars = new Dictionary<int, Jy123.Model.Public_Cars>();
            foreach (Jy123.Model.Public_Cars car in listCars)
            {
                dicPCars.Add(car.ID, car);
            }
            foreach (Jy123.Model.Club_Cars record in listRecord)
            {
                string strDetail = context.Cache["trackinfo_detail"].ToString();
                Jy123.Model.Public_Cars cPCar = dicPCars[record.FClubCarsType.Value];
                strDetail = strDetail.Replace("$DetailImageSrc$", cPCar.SmallModel);
                strDetail = strDetail.Replace("$DetailName$", cPCar.CName);
                strDetail = strDetail.Replace("$DetailEffects$", "速度");
                if (cPCar.Speed.HasValue)
                {
                    strDetail = strDetail.Replace("$DetailValue$", "+" + cPCar.Speed.Value.ToString());
                }
                else
                {
                    strDetail = strDetail.Replace("$DetailValue$", "无");
                }
                //string strDes = cPCar.Description;
                //if (strDes.Length > 7)
                //{
                //    strDes = strDes.Substring(0, 7) + "...";
                //}
                //strDetail = strDetail.Replace("$DetailUse$", strDes);                
                strDetail = strDetail.Replace("$DetailUse$", string.Empty);
                strDetail = strDetail.Replace("$Style$", "style='display:none'");
                string strEvent = string.Format("PostAjax('/Module/CarClub/CarInfo.ashx','Action=Use&ClubId={0}&Id={1}');", module.ID.ToString(), record.ID.ToString());
                strDetail = strDetail.Replace("$ClickEvent$", strEvent);
                strDetail = strDetail.Replace("$ImageSrc$", "/Images/Module/QC3icon2.gif");
                strResult.Append(strDetail);
            }
            return strResult.ToString();
        }

        private string BindDetail(List<Jy123.Model.Public_Cars> listCars, HttpContext context)
        {
            StringBuilder strResult = new StringBuilder();
            foreach (Jy123.Model.Public_Cars cPCar in listCars)
            {
                string strDetail = context.Cache["trackinfo_detail"].ToString();
                strDetail = strDetail.Replace("$DetailImageSrc$", cPCar.SmallModel);
                strDetail = strDetail.Replace("$DetailName$", cPCar.CName);
                strDetail = strDetail.Replace("$DetailEffects$", "速度");
                if (cPCar.Speed.HasValue)
                {
                    strDetail = strDetail.Replace("$DetailValue$", "+" + cPCar.Speed.Value.ToString());
                }
                else
                {
                    strDetail = strDetail.Replace("$DetailValue$", "无");
                }

                //string strDes = cPCar.Description;
                //if (strDes.Length > 7)
                //{
                //    strDes = strDes.Substring(0, 7) + "...";
                //}
                //strDetail = strDetail.Replace("$DetailUse$", strDes);
                strDetail = strDetail.Replace("$DetailUse$", string.Empty);
                string strEvent = string.Empty;
                //string usingLevel = cPCar.UsingLevel;
                BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                Model.Venues_Main venues = BLLVenues.GetModel(module.ID);
                BLL.Club_Cars BLLARecord = new Jy123.BLL.Club_Cars();
                List<Model.Club_Cars> listRecord = BLLARecord.GetModelList("FClub = " + module.ID.ToString());
                //bool canUse = string.IsNullOrEmpty(usingLevel) || usingLevel.Contains(venues.FLevel.Value.ToString());
                string strImageSrc = "/Images/Module/Buy.gif";
                if (user.Cash < cPCar.Price.Value)
                {
                    strEvent = "$.moonbox.warning('您的现金不足！')";
                }
                else if (IsInList(cPCar, listRecord))
                {
                    strEvent = "$.moonbox.warning('您已经拥有这件商品了！')";
                    strImageSrc = "/Images/Module/AlreadyBuy.gif";
                }
                //else if (!canUse)
                //{
                //    strEvent = "$.moonbox.warning('该商品不适合您的组件使用！')";
                //    strImageSrc = "/Images/Module/CanNotUse.gif";
                //}
                else
                {
                    strEvent = string.Format("PostAjax('/Module/CarClub/CarInfo.ashx','Action=Annexe&ClubId={0}&Id={1}');fillhtml('listUl','/Module/CarClub/CarInfo.ashx?Type=CarStore&ClubId='+$('#venuesHidden').val()+'&PType='+$('#selTypeHidden').val()+'&'+Math.random())", module.ID.ToString(), cPCar.ID.ToString());
                }
                strDetail = strDetail.Replace("$ImageSrc$", strImageSrc);
                strDetail = strDetail.Replace("$ClickEvent$", strEvent);
                strResult.Append(strDetail);
            }
            return strResult.ToString();
        }

        private bool IsInList(Model.Public_Cars car, List<Model.Club_Cars> listRecord)
        {
            foreach (Model.Club_Cars record in listRecord)
            {
                if (car.ID == record.FClubCarsType)
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
